<template>
    <a-layout>
        <a-layout-content :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }">
            <h3 v-if="level1.length === 0">对不起，找不到相关文档！</h3>
            <a-row>
                <a-col :span="6">
                    <a-tree
                        v-if="level1.length > 0"
                        :tree-data="level1"
                        @select="onSelect"
                        :replaceFields="{title: 'name', key: 'id'}"
                        :defaultExpandAll="true"
                        :defaultSelectedKeys="defaultSelectedKeys"
                    >
                    </a-tree>
                </a-col>
                <a-col :span="18">
                    <div>
                        <h2>{{doc.name}}</h2>
                        <div>
                            <span>阅读数：{{doc.viewCount}}</span>&nbsp;&nbsp;
                            <span>点赞数：{{doc.voteCount}}</span>
                        </div>
                    </div>
                    <a-divider style="height: 2px; background-color: #9999cc"/>
                    <div class="wangeditor" :innerHTML="html"></div>
                    <div class="vote-div">
                        <a-button type="primary" shape="round" size="large" @click="handleVote">
                            <template #icon>
                                <LikeOutlined />&nbsp;点赞：{{doc.voteCount}}
                            </template>
                        </a-button>
                    </div>
                </a-col>
            </a-row>
        </a-layout-content>
    </a-layout>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref } from 'vue';
import axios from 'axios';
import { message } from 'ant-design-vue';
import {Tool} from "@/util/tool";
import {useRoute} from "vue-router";

export default defineComponent({
    name: 'Doc',
    setup() {
        const route = useRoute();
        const html = ref() ;

        /**
         * 一级文档树，children属性就是二级文档
         * [{
         *   id: "",
         *   name: "",'
         *   ...
         *   children: [{
         *     id: "",
         *     name: "",
         *     ...
         *   }]
         * }]
         */
        const level1 = ref(); // 一级文档树，children属性就是二级文档
        level1.value = [];       // 因为html中用到了level1.length，若不初始化一个空数组，页面加载时会报错

        const defaultSelectedKeys = ref() ;
        // 当前选中的文档
        const doc = ref() ;
        doc.value = {} ;

         /** 文档内容查询（参数：文档id） */
        const handleQueryContent = (id: number) => {
            axios.get("/doc/find-content/" + id).then((response) => {
                const data = response.data ;
                if(data.success){
                    html.value = data.content ;
                }else {
                    message.error(data.message);
                }
            });
        };

        /** 数据查询 */
        const handleQuery = () => {
            axios.get("/doc/all/" + route.query.ebookId).then((response) => {
                const data = response.data ;
                if(data.success){
                    const docs = data.content ;
                    level1.value = Tool.array2Tree(docs, 0) ; // 父id为0，表示根节点，即一级文档

                    // 若文档树不为空，则默认选择第一个文档进行右边内容的展示
                    if(Tool.isNotEmpty(level1)) {
                        defaultSelectedKeys.value = [level1.value[0].id] ; // 设置左侧文档树选中效果（注意，值要是数组类型）
                        handleQueryContent(level1.value[0].id) ;    // 根据文档id，查询右侧文档内容
                        // 初始显示文档信息
                        doc.value = level1.value[0] ;
                    }

                }else {
                    message.error(data.message);
                }
            });
        };

        /** 选择文档id */
        const onSelect = (selectedKeys: any, info: any) => {
            console.log("onSelect: ", selectedKeys, info) ;
            if(Tool.isNotEmpty(selectedKeys)){
                // 选中某一节点时，加载该节点的文档信息
                doc.value = info.selectedNodes[0].props ;
                // 加载文档内容
                handleQueryContent(selectedKeys[0]);
            }
        };

        /** 点赞文档 */
        const handleVote = () => {
            axios.get("/doc/vote/" + doc.value.id).then((response) => {
                const data = response.data ;
                if(data.success){
                    doc.value.voteCount++ ;
                }else {
                    message.error(data.message);
                }
            });
        };

        onMounted(() => {
            handleQuery() ;
        });

        return {
            level1,
            onSelect,
            html,
            defaultSelectedKeys,
            doc,
            handleVote
        };
    },

});
</script>

<!-- 去掉了scoped，以便下面的样式对所有vue页面生效，这样admin-doc.vue预览内容时也可以用 -->
<!--<style scoped>-->
<style>
    /* wangeditor默认样式, 参照: https://www.wangeditor.com/doc/pages/02-%E5%86%85%E5%AE%B9%E5%A4%84%E7%90%86/03-%E8%8E%B7%E5%8F%96html.html */
    /* 注意：前面加上 .wangeditor ， 只针对富文本区域生效，否则会对页面所有样式造成影响 */
    /* table 样式 */
    .wangeditor table {
      border-top: 1px solid #ccc;
      border-left: 1px solid #ccc;
    }
    .wangeditor table td,
    .wangeditor table th {
      border-bottom: 1px solid #ccc;
      border-right: 1px solid #ccc;
      padding: 3px 5px;
    }
    .wangeditor table th {
      border-bottom: 2px solid #ccc;
      text-align: center;
    }

    /* blockquote 样式 */
    .wangeditor blockquote {
      display: block;
      border-left: 8px solid #d0e5f2;
      padding: 5px 10px;
      margin: 10px 0;
      line-height: 1.4;
      font-size: 100%;
      background-color: #f1f1f1;
    }

    /* code 样式 */
    .wangeditor code {
      display: inline-block;
      *display: inline;
      *zoom: 1;
      background-color: #f1f1f1;
      border-radius: 3px;
      padding: 3px 5px;
      margin: 0 3px;
    }
    .wangeditor pre code {
      display: block;
    }

    /* ul ol 样式 */
    .wangeditor ul, ol {
      margin: 10px 0 10px 20px;
    }

    /* 和antdv p冲突，覆盖掉 */
    .wangeditor blockquote p {
      font-family:"YouYuan";
      margin: 20px 10px !important;
      font-size: 16px !important;
      font-weight:600;
    }

    /* 图片自适应 */
    .wangeditor img {
      max-width: 100%;
      height: auto;
    }

    /* 视频自适应 */
    .wangeditor iframe {
        width: 100%;
        height: 400px;
    }

    .vote-div {
        padding: 15px;
        text-align: center;
    }
</style>